package com.zhang.controller;


import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.entity.ShoppingCategory1;
import com.zhang.entity.ShoppingCategory2;
import com.zhang.service.ShoppingCategory1Service;
import com.zhang.service.ShoppingCategory2Service;
import com.zhang.service.ShoppingProductService;
import com.zhang.units.Result;
import com.zhang.vo.Category1Vo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZhangD
 * @since 2021-07-13
 */
@RestController
@RequestMapping("/shopping-category1")
public class ShoppingCategory1Controller {

    @Autowired
    private ShoppingCategory1Service shoppingCategory1Service;
    @Autowired
    private ShoppingCategory2Service shoppingCategory2Service;
    @Autowired
    private ShoppingProductService shoppingProductService;
    /**
     * 查询一级目录
     */

    @GetMapping("/Category1")
    public Result selectAllCategory1(){
        List<ShoppingCategory1> shoppingCategory1s = shoppingCategory1Service.selectShoppingCategory1();
        Result result = Result.result200(shoppingCategory1s);
        return result;
    }
    /**
    *根据一级类目查找该一级类目下的所有商品,以及所有的二级类目
    */
    @GetMapping("/category2")
    public Result selectAllCategory2(@RequestParam("id") int id,
                                     @RequestParam("pageNum")int pageNum,
                                     @RequestParam("pageSize")int pageSize){
        Page<ShoppingCategory2> shoppingCategory2s = shoppingCategory2Service.selectShoppingCategory2(id, pageNum, pageSize);
        return Result.result200(shoppingCategory2s);
    }
    /**
    *根据二级类目查找该二级类目下的所有商品
    */
    @GetMapping("/category2/{id}/{pageNum}/{pageSize}")
    public  Result selectAllProductByCategory2(@PathVariable("id") int id,
                                               @PathVariable("pageNum")int pageNum,
                                               @PathVariable("pageSize")int pageSize){
        return Result.result200(shoppingProductService.selectShoppingProductByCategory2Id(id,pageNum,pageSize));
    }
    /**
    *商品详情
     */
    @GetMapping("/product/{id}")
    public Result selectProductByProductByProductId(@PathVariable("id") int id){
        return Result.result200(shoppingProductService.selectShoppingProductByProductId(id));
    }
    /**
     * 显示一级类目列表
     */
    @GetMapping("/category1_name")
    public  Result  category1Name(@RequestParam("category1Name") String category1Name,
                                  @RequestParam("pageNo") int pageNo,
                                  @RequestParam("pageSize")int pageSize){
        return Result.result200(shoppingCategory1Service.category1Name(category1Name,pageNo,pageSize));
    }
    /**
     * 处理显示或者隐藏一级类目
     */
    @GetMapping("/change_value1")
    public  Result changeValue(@RequestParam("category1Id") String category1Id){
            return Result.result200(shoppingCategory1Service.changeValue(category1Id));
    }
    /**
     * 详细信息
     */
    @GetMapping("/get_category1")
    public Result getCategory(@RequestParam("category1Id") String category1Id){
        return Result.result200(shoppingCategory1Service.getCategory(category1Id));
    }
    /**
     * 新增一级目录
     */
    @GetMapping("/add_category1")
    public Result addCategory1(@RequestParam("category1Name") String category1Name,
                               @RequestParam("category1Record")String category1Record){
        return Result.result200(shoppingCategory1Service.addCategory1(category1Name,category1Record));
    }
    /**
     *修改一级类目
     */
    @PostMapping("/update_category1")
    public Result updateCategory1(@RequestBody Category1Vo category1Vo){
        return Result.result200(shoppingCategory1Service.updateCategory1(category1Vo.getId(), category1Vo.getCategory1Name(), category1Vo.getCategory1Record()));
    }
    /**
     * 删除一级类目
     */
    @GetMapping("/delete_category1")
    public  Result deleteCategory1(@RequestParam("category1Id") String category1Id){
        return Result.result200(shoppingCategory1Service.removeById(category1Id));
    }
    /**
     * 删除一级类目
     */
    @GetMapping("/delete_category2")
    public  Result deleteCategory2(@RequestParam("category1Ids") List<String> category1Ids){
        return Result.result200(shoppingCategory1Service.removeByIds(category1Ids));
    }
}

